#ifndef _SOCKET_CONN_POOL_H_
#define _SOCKET_CONN_POOL_H_

#include <vector>
#include <string>

#include <log4cxx/logger.h>

#include <pthread.h>

#include "SocketConnPtr.h"

using std::vector;
using std::string;

using namespace log4cxx;

struct SOCKET_CONN_POOL_ITEM {
	SOCKET_CONN_POOL_ITEM() { fd = -1; is_in_use = false; }
	int fd;
	bool is_in_use;
	bool is_network_error;
};

class SocketConnPool {

	public:
		int init(const char *ip, const unsigned short port, const int conn_timeout, const int pool_size);
		int get(SocketConnPtr & ptr);
		int release(int index, bool is_network_error);
	protected:
		int reconnect(int index);
	private:
		vector<SOCKET_CONN_POOL_ITEM> _conns;
		pthread_mutex_t _mutex;
		string _ip;
		unsigned int _port;
		int _conn_timeout;
		int _pool_size;
		static LoggerPtr _logger;

};

#endif
